<template>
  <div>
    <el-dialog
      v-dialogDrag
      ref="table"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogFormVisible"
      :append-to-body="true"
      title="维修项目选择"
      width="70%"
      @opened="searchHandle"
      @close="cancel" >
      <el-form ref="searchForm" :inline="true" :model="search" size="mini">
        <el-form-item prop="repairGroupTypeId" label="组合类别">
          <r-select v-model="search.repairGroupTypeId" type="1200" placeholder="请选择" class="form-input"/>
        </el-form-item>
        <el-form-item prop="repairGroupCode" label="组合代码">
          <el-input v-model="search.repairGroupCode" placeholder="组合代码" class="search-input"/>
        </el-form-item>
        <el-form-item prop="repairGroupName" label="组合名称">
          <el-input v-model="search.repairGroupName" placeholder="组合名称" class="search-input"/>
        </el-form-item>
        <br>

        <div style="text-align:center">
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="searchHandle">查询
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-refresh"
              @click="resetForm('searchForm')">重置
            </el-button>
          </el-form-item>
          <!-- <el-form-item>
        <el-button type="primary"
                   icon="el-icon-plus"
                   @click="add"
        >新增
        </el-button>
      </el-form-item> -->
        </div>
      </el-form>
      <!-- 工种列表-->
      <div class="tableHead">
        <div class="title">工种定义信息</div>
      </div>
      <baseTable
        ref="table1"
        @chaneg-size="changeSizeHandle"
        @chaneg-page="changePageHandle"
      >
        <el-table-column align="center" label="序号" width="50" type="index"/>
        <el-table-column align="center" label="操作" min-width="200">
          <template slot-scope="scope">
            <el-radio v-model="radio" :label="scope.$index" @change.native="Choice(scope.$index,scope.row)" >&nbsp;</el-radio>
          </template>
        </el-table-column>
        <el-table-column
          prop="BASEDATA_CODE"
          label="组合代码"
          min-width="150"/>
        <el-table-column
          prop="REPAIR_GROUP_NAME"
          label="组合名称"
          min-width="150"
        />
        <el-table-column
          prop="BASEDATA_NAME"
          label="组合类型"
          min-width="100"/>
        <el-table-column
          prop="DATA_SOURCES"
          label="数据来源"
          min-width="100">
          <template slot-scope="scope">
            <filtercode
              v-model="scope.row.DATA_SOURCES"
              type="1045"
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="IS_VALID"
          label="是否有效"
          min-width="150">
          <template slot-scope="scope">
            <filtercode
              v-model="scope.row.IS_VALID"
              type="1004"
            />
          </template>
        </el-table-column>
      </BaseTable>

      <div class="tableHead">
        <div class="title">维修项目</div>
      </div>
      <BaseTableNoPage
        ref="childTable1"
        stripe
        border
        @chaneg-size="changeSizeHandle"
        @chaneg-page="changePageHandle">
        <el-table-column label="序号" type="index" min-width="10"/>
        <el-table-column prop="labourCode" label="维修项目代码"/>
        <el-table-column prop="labourName" label="维修项目名称"/>
        <el-table-column prop="chargePartitionCode" label="收费区分"/>
        <el-table-column prop="repairTypeCode" label="维修分类"/>
        <el-table-column prop="stdLabourHour" label="标准工时"/>
        <el-table-column prop="assignLabourHour" label="派工工时"/>
        <el-table-column prop="claimLabour" label="索赔工时"/>
        <el-table-column prop="workHourSinglePrice" label="工时单价"/>
        <el-table-column prop="modelLabourCode" label="维修组合代码"/>
        <!-- <el-table-column prop="REPAIR_GROUP_LABOUR_ID" label="维修机型组名称">
              </el-table-column> -->
      </BaseTableNoPage>
      <div class="tableHead">
        <div class="title">维修配件</div>
      </div>
      <BaseTableNoPage
        ref="childTable2"
        stripe
        border
        @chaneg-size="changeSizeHandle"
        @chaneg-page="changePageHandle">
        <el-table-column label="序号" type="index" min-width="10"/>
        <el-table-column prop="chargePartitionCode" label="收费区分"/>
        <el-table-column prop="storageCodeShow" label="仓库代码"/>
        <el-table-column prop="partCodeShow" label="配件代码"/>
        <el-table-column prop="partNameShow" label="配件名称"/>
        <el-table-column prop="canNumShow" label="配件数量"/>
        <el-table-column prop="salesPriceShow" label="配件销售价"/>
      </BaseTableNoPage>

      <span slot="footer" class="dialog-footer" >
        <el-button type="text"><i class="el-icon-search magnifier"/></el-button>
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="resetProject" >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRepairGroup, getDetails1, getDetails2 } from '@/api/repair/basedata/repairgroupmanage/repairgroup'
import BaseTable from '@/components/BaseTable'
import BaseDialog from '@/components/BaseDialog'
import WorkerTypesSelect from '@/components/Select/workerTypesSelect'
import BaseTableNoPage from '@/components/BaseTable/BaseTableNoPage'
import rSelect from '@/components/Select/Select'
import checkbox from '@/components/Checkbox/checkbox'
import filtercode from '@/components/Checkbox/filtercode'
import formMixins from '@/mixins/formMixins'

export default {
  name: 'Repairgroup',
  components: {
    BaseTable,
    BaseDialog,
    WorkerTypesSelect,
    rSelect,
    checkbox,
    filtercode,
    BaseTableNoPage
  },
  mixins: [formMixins],
  props: {
    dataItem: Array
  },
  data() {
    return {
      model: ['1', '2'],
      active: 1,
      type1: 1,
      dialogFormVisible: true,
      radio: '',
      search: {
        limit: 10,
        pageNum: 1,
        repairGroupTypeId: '',
        repairGroupCode: '',
        repairGroupName: ''
      }
    }
  },
  computed: {
  },
  created() {
    this.$nextTick(() => {
      this.searchHandle()
      this.visitDate = new Date()
    })
  },
  methods: {
    // 主页面table 查询
    searchHandle() {
      const table = this.$refs['table1']
      getRepairGroup(this.search)
        .then(res => {
          table.complete().filData(res)
          this.dealercode = res.rows[0].DEALER_CODE
        })
        .catch(err => {
          table.complete()
        })
    },
    resetProject() {
      var repairPro = this.$refs['childTable1'].list
      var isDouble = false
      var lastRepairPro = this.$props.dataItem
      if (lastRepairPro.length > 0) {
        for (var index = 0; index < repairPro.length; index++) {
          var currentLabour = repairPro[index].labourCode + '|' + repairPro[index].modelLabourCode
          for (var i = 0; i < lastRepairPro.length; i++) {
            var lastLabour = lastRepairPro[i].labourCode + '|' + lastRepairPro[i].modelLabourCode
            if (lastLabour === currentLabour) {
              isDouble = true
            }
          }
        }
      }
      if (isDouble) {
        this.$message({
          type: 'info',
          message: '重复添加'
        })
      } else {
        this.confirmHandle()
      }
    },

    confirmHandle() {
      let index = 1
      const orderNum = this.$props.dataItem.length
      const LabourList = this.$refs['childTable1'].list
      LabourList.forEach(el => {
        this.$set(el, 'labourPrice', el.workHourSinglePrice)
        this.$set(el, 'assignLabourHour', el.assignLabourHour)
        this.$set(el, 'stdLabourHour', el.stdLabourHour)
        this.$set(el, 'orderNum', orderNum + index)
        this.$set(el, 'discount', 1)
        this.$emit('Labour', el)
        index++
      })
      const childTable2 = this.$refs['childTable2'].list
      childTable2.forEach(el => {
        LabourList.forEach(el1 => {
          if (el1.labourCode === el.labourCode) {
            el.orderNum = el1.orderNum
            el.labourName = el1.labourName
            el.chargePartitionCode = el1.chargePartitionCode
          }
        })
        el.available = el.AVAILABLE
        el.costAmount = el.COST_AMOUNT
        el.costPrice = el.COST_PRICE
        el.dealerCode = el.DEALER_CODE
        el.labourPartId = el.LABOUR_PART_ID
        el.modelLabourCode = el.MODEL_LABOUR_CODE
        el.modelLabourName = el.MODEL_LABOUR_NAME
        el.partName = el.partNameShow
        el.partNo = el.partCodeShow
        this.$set(el, 'partQuantity', el.canNumShow)
        this.$set(el, 'partSalesPrice', el.salesPriceShow)
        el.partStatus = el.PART_STATUS
        el.partStockId = el.PART_STOCK_ID
        el.stockQuantity = el.STOCK_QUANTITY
        el.storageCode = el.storageCodeShow
        el.storageName = el.storageCodeShow
        el.storagePositionCode = el.STORAGE_POSITION_CODE
        el.unit = el.UNIT
        el.discount = 1
      })
      this.$message({
        type: 'success',
        message: '添加成功'
      })
      this.$emit('LaPart', childTable2)
    },
    Choice(index, data) {
      const id = data.REPAIR_GROUP_ID
      this.searchHandle1(id)
      this.searchHandle2(id)
    },
    searchHandle1(id) {
      const table1 = this.$refs['childTable1']
      table1.wait()
      getDetails1(id)
        .then(res => {
          console.log(res)
          table1.complete().filData(res)
          // this.dealercode = res.rows[0].DEALER_CODE;
        })
        .catch(err => {
          table1.complete()
        })
    },
    // 一层弹窗table查询
    searchHandle2(id) {
      const table2 = this.$refs['childTable2']
      table2.wait()
      getDetails2(id)
        .then(res => {
          console.log(res)
          table2.complete().filData(res)
          // this.dealercode = res.rows[0].DEALER_CODE;
        })
        .catch(err => {
          table2.complete()
        })
    },
    cancel() {
      this.$emit('cancel1', 1)
    }
  }
}
</script>

<style scoped>
.dialog-form {
  width: 100%;
  margin: 0;
}

.el-checkbox-group .el-checkbox + .el-checkbox {
  margin: 0;
}

</style>
